package cn.e2win.biz.util;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.e2win.biz.sys.Dict;


public class DateUtils {
	public static final String YYYYMMDD = "yyyy-MM-dd";
	public static final String YYYYMMDDHMS = "yyyy-MM-dd HH:mm:ss";

	public static DateUtils Instance = new DateUtils();
	public DateUtils(){
		
	}
	public static Date getOffsetDay(Date date,int day){
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.DAY_OF_YEAR,calendar.get(Calendar.DAY_OF_YEAR)+day);
		Date tempDate = calendar.getTime();
		return str2Date(date2Str(tempDate,YYYYMMDD));
	}
	
	public static Date getOffsetMonth(Date date,int month){
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.MONTH,calendar.get(Calendar.MONTH)+month);
		Date tempDate = calendar.getTime();
		return str2Date(date2Str(tempDate,YYYYMMDD));
	}
	public static String getOffsetMin(Date date,int min){
		if(date == null) return "";
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.MINUTE,calendar.get(Calendar.MINUTE)+min);
		Date tempDate = calendar.getTime();
		return date2Str(tempDate,YYYYMMDDHMS);
	}
	
	public static Date getPreMonth(){
		Calendar calendar = Calendar.getInstance();
		calendar.setTimeInMillis(System.currentTimeMillis());
		calendar.set(Calendar.MONTH,calendar.get(Calendar.MONTH)-1);
		calendar.set(Calendar.DAY_OF_MONTH, 1);
		Date tempDate = calendar.getTime();
		return str2Date(date2Str(tempDate,YYYYMMDD));
	}
	
	public static Date getNextMonth(){
		Calendar calendar = Calendar.getInstance();
		calendar.setTimeInMillis(System.currentTimeMillis());
		calendar.set(Calendar.MONTH,calendar.get(Calendar.MONTH)+1);
		calendar.set(Calendar.DAY_OF_MONTH, 1);
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND,0);
		calendar.set(Calendar.MILLISECOND,0);
		return calendar.getTime();
	}

	public static Date str2Date(String strDate,String format){
		if(StringUtils.isEmpty(strDate)){
			return null;
		}
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		try {
			return sdf.parse(strDate);
		} catch (ParseException e) {
			return null;
		}
	}
	public static Date str2Date(String strDate){
		if(StringUtils.isEmpty(strDate)){
			return null;
		}
		SimpleDateFormat sdf = new SimpleDateFormat(YYYYMMDD);
		try {
			return sdf.parse(strDate);
		} catch (ParseException e) {
			return null;
		}
	}
	
	public static Timestamp str2Timestamp (String tsStr){
		Timestamp ts = null;
		//tsStr = "2011-05-09 11:49:45";
		tsStr = tsStr.trim();
		try { 
			if(!StringUtils.isEmpty(tsStr)){
				Date date = DateUtils.str2Date(tsStr);
				tsStr = DateUtils.date2Str(date,YYYYMMDDHMS);
				ts = Timestamp.valueOf(tsStr);	
			}

			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ts;
	}


	public static Timestamp str2Timestamp (String tsStr, String fmt){
		Timestamp ts = null;
		//tsStr = "2011-05-09 11:49:45";
		tsStr = tsStr.trim();
		try { 
			if(!StringUtils.isEmpty(tsStr)){
				Date date = DateUtils.str2Date(tsStr, fmt);
				tsStr = DateUtils.date2Str(date,fmt);
				ts = Timestamp.valueOf(tsStr);	
			}

			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ts;
	}

	public static java.sql.Date str2SqlDate(String strDate){
		Date d = str2Date(strDate);
		if (d==null){
			return null;
		}else{
			return new java.sql.Date(d.getTime());
		}
	}
	
	
	public static Date monthfirstDate(int year,int month){
		Calendar calendar = Calendar.getInstance();
		calendar.set(year, month, 1);
		Date tempDate = calendar.getTime();
		return str2Date(date2Str(tempDate,YYYYMMDD));
	}
	
	public static Date getDayOfSunday(Date date){
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		if(calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY){
			return date;
		}else{
			calendar.set(Calendar.DAY_OF_WEEK,Calendar.SUNDAY);
			return getOffsetDay(calendar.getTime(), 7);
		}
	}
	
	public static Date getDayOfStaturaday(Date date){
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		if(calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY){
			return getOffsetDay(date, -1);
		}else{
			calendar.set(Calendar.DAY_OF_WEEK,Calendar.SATURDAY);
			Date tempDate = calendar.getTime();
			return str2Date(date2Str(tempDate,YYYYMMDD));
		}
	}
	
	public static int compare(Date begin,Date end){
		Calendar beginC = Calendar.getInstance();
		beginC.setTime(begin);
		Calendar endC = Calendar.getInstance();
		endC.setTime(end);
		return endC.get(Calendar.DAY_OF_YEAR) - beginC.get(Calendar.DAY_OF_YEAR);
	}
	
	public static List<Date> range(Date begin,Date end){
		int offset = compare(begin, end);
		List<Date> range = new ArrayList<Date>();
		for(int i = 0;i<offset;i++){
			range.add(getOffsetDay(begin, i));
		}
		range.add(end);
		return range;
	}
	
	public static String rangeMd(Date begin,Date end){
		List<Date> rangeDate = range(begin,end);
		StringBuffer rangeMdStr = new StringBuffer();
		for(Date d : rangeDate){
			rangeMdStr.append(String.format("'%1$tm%1$td',", d));
		}
		return rangeMdStr.toString().replaceAll("\\,$", "");
	}
	
	public static String rangeMM(Date begin,Date end){
		String bm = String.format("%1$tm", begin),
		   em = String.format("%1$tm", end);
		StringBuffer rangeMdStr = new StringBuffer();
		int b = Integer.valueOf(bm),e = Integer.valueOf(em), i = 0;
		i = e == b?0:(e > b? e - b:e-b+12);
		for(int j = 0;j<i;j++)
		{
			int m = b+j;
			if(m > 12){
				m-=12 ;
			}
			rangeMdStr.append(String.format("'%02d',", m));
		}
		return rangeMdStr.append("'"+em+"'").toString();
	}
	

	
	public static java.sql.Date parse2SqlDate(Date date){
		return new java.sql.Date(date.getTime());
	}
	
	public static String date2Str(Date date, String format){
		SimpleDateFormat sdf=new SimpleDateFormat(format);  
		return sdf.format(date); 
	}

	public static String date2Str(Date date){
		SimpleDateFormat sdf=new SimpleDateFormat(YYYYMMDD);  
		if(date!=null){
			return sdf.format(date);
		}else{
			return "";
		}
	}
	//获取当前的系统时间
	public static Date getNow(){
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(new Date(System.currentTimeMillis()));
		Date date = calendar.getTime();
		return str2Date(date2Str(date,YYYYMMDD));
	}
	public static Boolean isToday(String strDate){
		Date today = getNow();
		Date date = str2Date(strDate, YYYYMMDD);
		return date!=null && date2Str(date, YYYYMMDD).equals(date2Str(today, YYYYMMDD));
	}
	
	public static int getIntersection(Date workdayBegin, Date workdayEnd,
			Date holidayBengin, Date holidayEnd){
		int wb = DateUtils.date2num(workdayBegin), we = DateUtils
		.date2num(workdayEnd), hb = DateUtils
		.date2num(holidayBengin), he = DateUtils
		.date2num(holidayEnd);
		int intersection = 0;
		if(wb >= he){
			intersection = 0;
		}else if(hb > we){
			intersection = 0;
		}
		else if(he >= we && wb >= hb){
			intersection = DateUtils.compare(workdayBegin, workdayEnd);
		}else if(we >= he && hb >= wb){
			intersection = DateUtils.compare(holidayBengin, holidayEnd);
		}else if(we >= hb && hb >= wb){
			intersection = DateUtils.compare(holidayBengin, workdayEnd);
		}else if(he >= wb && wb >= hb){
			intersection = DateUtils.compare(workdayBegin, holidayEnd);
		}
		return intersection;
	}
	
	//获取系统的昨天
	public static Date getYesterday(){
		return DateUtils.getOffsetDay(DateUtils.getNow(), -1);
	}
	
	public static String fulFrmtDate(Long time){
		return String.format("%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$TS", time);
	}
	
	public static String fulFrmtDate(Timestamp time){		
		return time == null?"":fulFrmtDate(time.getTime());
	}
	
	public static int date2num(Date date){
		return Integer.valueOf(String.format("%1$tY%1$tm%1$td", date));
	}
	
	public static List<Map<String, String>> gePerSixMonths() {
		List<Map<String, String>> list = new ArrayList<Map<String,String>>();
		Calendar calendar = Calendar.getInstance();
		for (int i=0; i<6; i++) {
			Map<String, String> month = new HashMap<String, String>();
			calendar.add(Calendar.MONTH,-1);
			month.put("key",date2Str(calendar.getTime(), "yyyy-MM"));
			month.put("value",date2Str(calendar.getTime(), "yyyy-MM"));
			list.add(month);
		}
		return list;
	}
	
	public static Timestamp getCustomDayE(int num) {
		Calendar calendar = Calendar.getInstance();//当前时间
		calendar.add(Calendar.DAY_OF_MONTH,num);//月中天
		calendar.set(Calendar.HOUR_OF_DAY, 24);//天中时
		return new Timestamp(calendar.getTime().getTime());
	}
	
	
	public static Timestamp getCustomDayB(int num) {
		Calendar calendar = Calendar.getInstance();//当前时间
		calendar.add(Calendar.DAY_OF_MONTH,num);//月中天
		calendar.set(Calendar.HOUR_OF_DAY, 0);//天中时
		calendar.set(Calendar.MINUTE, 0);//天中时
		return new Timestamp(calendar.getTime().getTime());
	}
}
